package com.example.lab04.controller;

import com.example.lab04.entity.Comment;
import com.example.lab04.entity.Member;
import com.example.lab04.entity.Product;
import com.example.lab04.service.CommentService;
import com.example.lab04.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController extends BaseController {
    public final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @GetMapping("query_comment_member_product")
    @ResponseBody
    private List<Comment> queryCommentMemberProduct() {
        List<Comment> comments = commentService.queryList("query_comment_member_product", null);
        for (Comment comment : comments) {
//            comment.setPictures(comment.getPictures().replaceAll());
        }
        return comments;
    }

    @PostMapping("create")
    @ResponseBody
    private boolean create(@RequestPart MultipartFile[] files, @RequestPart Comment comment) {
        Member member = (Member) session.getAttribute("member");
        if (member == null) {
            return false;
        }
        Product product = (Product) session.getAttribute("product");
        //获取外键
        comment.setMemberId(member.getId());
        comment.setProductId(product.getId());

        comment.setIp(request.getRemoteAddr());
        String directory = "D:/lab04/comment";
        comment.setPictures(UploadUtil.savePicture(files, directory));

//        System.out.println(Arrays.toString(files));
//        System.out.println(content);
        commentService.create(comment);
        return true;
    }

}

